# -*- coding:utf-8 -*-
# @Author:Cheng Lei 1037654919@qq.com
# @Time : 2023/3/29 下午2:54
# @FileName: kuwo_t5_song_base_singer.py
# @Software: PyCharm
import math

# 第五 新增歌手
import requests
import time
import random
from utils import MongoDBUtil,get_html,mongo_manager
from multiprocessing import Pool
from class_kuwo import Kuwo
MongoDBUtil = MongoDBUtil()
playlist_tag = mongo_manager("playlist_tag", db="kuwo")
playlist_song= mongo_manager("playlist_song", db="kuwo")
playlist_song_list= mongo_manager("playlist_song_list", db="kuwo")
singer_list = mongo_manager("singer_list", db="kuwo")
song_info= mongo_manager("song_info", db="kuwo")

def get_proxy():
    port = random.randint(24000, 24400)
    proxies = {'http': f'http://zheng123:zheng123@haproxy.iinti.cn:{port}',
               'https': f'http://zheng123:zheng123@haproxy.iinti.cn:{port}'}
    return proxies


kuwo = Kuwo()

# 更新歌曲列表
def playlist_song_list_run():
    seeds = playlist_song.findAll({"status": None})
    for seed in seeds:
        result ={"_id":seed['rid']}
        try:
            # print(data)
            MongoDBUtil.insert_one('playlist_song_list', result)
        except Exception as e:
            print(e)
        seed['status']='success'
        playlist_song.updateOne({'_id': seed['_id']}, seed)


# 插入数据
def insert_data(tablename, datas):
    if len(datas) > 0 and datas is not None:
        for data in datas:
            # data['_id'] = data['id']
            try:
                # print(data)
                MongoDBUtil.insert_one(tablename, data)
            except Exception as e:
                print(e)
    else:
        pass


# 解析歌曲信息
def parse_singer_songs(data):
    datass = data['list']
    results=[]
    for datas in datass:
        result = {'_id': str(datas.get("artistid"))+'_'+str(datas.get("rid")),
                  'musicrid': datas.get("musicrid"),
                  'artist': datas.get("artist"),
                  'artistid': datas.get("artistid"),
                  'releasedate': datas.get("releasedate"),
                  'albumid': datas.get("albumid"),
                  'rid': datas.get("rid")
                  }
        results.append(result)
    return results

# 临时爬取部分数据
def test_xuqqiu(artist):
    print(artist)
    seeds = song_info.findOne({"artist": artist})
    song_info.close()
    print(seeds['artistid'])
    try:
        artistid = seeds['artistid']
        for page in range(1, 4):
            datas = kuwo.get_artist_Music(artistid=artistid, pn=page)
            results = parse_singer_songs(datas)
            insert_data(tablename='singer_song_20230511_linshi', datas=results)
    except Exception as e:
        print(e)


# 获取歌曲信息
def artist_Music(seed):
    singer_list = mongo_manager("singer_list", db="kuwo")
    artistid = seed['id']
    datas = kuwo.get_artist_Music(artistid=artistid)
    pages = math.ceil(int(datas['total'])/20)
    for page in range(1,pages+1):
        datas = kuwo.get_artist_Music(artistid=artistid, pn=page)
        results = parse_singer_songs(datas)
        insert_data(tablename='singer_song', datas=results)

    seed['status'] = 'success'
    singer_list.updateOne({'_id': seed['_id']}, seed)
    singer_list.close()

    print('{} Done'.format(seed['id']))

# 获取歌曲信息 run
def artist_Music_run():
    seeds = singer_list.findAll({"status": None})
    lists =[]
    for seed in seeds:
        lists.append(seed)
    print('singer_list None:',len(lists))
    singer_list.close()
    pool = Pool(processes=32)
    pool.map(artist_Music, lists)
    pool.close()  # 关闭进程池，不再接受新的进程
    pool.join()

if __name__ == '__main__':

    # artistid = 680
    # pn = 1
    #
    # datas = kuwo.get_artist_Music(artistid=artistid, pn=1)
    # results = parse_singer_songs(datas)
    # insert_data(tablename='singer_song', datas=results)
    # print(results)

    # run
    # artist_Music_run()

    # seeds = singer_list.findAll({"status": None})
    #
    # for seed in seeds:
    #     seed['status'] = 'success'
    #     singer_list.updateOne({'_id': seed['_id']}, seed)

    # linshi test
    lists = ['周杰伦', 'G.E.M.邓紫棋', '林俊杰', '陈奕迅', '张学友', '薛之谦', 'Beyond', '张杰', '王菲', '刘德华',
             '赵雷', '周传雄', '许巍', '任贤齐', '李克勤', '张信哲', '五月天', '李宗盛', '王心凌', '蔡依林', '李玉刚',
             '朴树', '杨千嬅', '齐秦', '潘玮柏', '林志炫', '胡彦斌', '陶喆', '汪峰', '孙燕姿', '庾澄庆', '许嵩',
             '陈小春', '蔡健雅', '林子祥', '郭顶', '郑智化', '萧亚轩', 'Justin Bieber', '李宇春', '伍佰', '古巨基',
             '杨坤', '徐良', '李圣杰', '郑伊健', '周深', '毛不易', '莫文蔚', '程响', '陈慧娴', '郁可唯', 'Alan Walker',
             'Avicii', '王力宏', '金志文', '谢霆锋', '张敬轩', '华晨宇', '张震岳', '罗大佑', '吴青峰', '周柏豪', '那英',
             '孙楠', '草蜢', '胡夏', '陈粒', '张惠妹', '郑少秋', '马頔', '卢巧音', '黑豹乐队', '陈柏宇', '吴克群',
             '江语晨', '王嘉尔', '任嘉伦', '苏打绿', 'Lil Nas X', 'Taylor Swift Tribute Band', '温岚', 'By2', '萧敬腾',
             '张国荣', '张靓颖', '庄心妍', '杨宗纬', '刘欢', '马天宇', '谭咏麟', '窦唯', 'S.H.E', '张雨生', '方大同',
             '尚雯婕', '张宇', 'Michael Jakson', 'Olly Murs', 'Martin Garrix', 'Sia', 'Kelly Clarkson', 'Billie Eilish',
             'P!nk', 'LiSA', 'HENRY刘宪华', '朴灿烈', '坂本龍一', '米津玄师', '筷子兄弟', '逃跑计划', '张碧晨',
             '刘若英', '张韶涵', '王靖雯', '周华健', '刀郎', '韩红', '满舒克', '梁静茹', '胡海泉', '陈势安', '许冠杰',
             '张楚生', '袁娅维TIA RAY', '田馥甄', '郑秀文', '杨培安', '信乐队', 'GALA', '焦迈奇', '艾福杰尼',
             '吉克隽逸', '戴佩妮', '张艺兴', '鹿晗', '周兴哲', '蔡徐坤', '老狼', '花粥', '郝云', '毛宁', '王若琳',
             '汪苏泷', '陈绮贞', '姜育恒', 'MISSKO', '容祖儿', '阿杜', '郑钧', '风凰传奇', '热狗', 'CoCo李玟', '大冢爱',
             '庞龙', 'Imagine Dragons', 'Maroon 5', 'Westlife', 'Linkin Park', 'Professor Green ', 'Ed Sheeran',
             'Charlie Puth', 'G-Dragon', 'Adele', 'Madonna', 'Rihanna', 'Avril Lavigne', '曲婉婷', '黎明', '卢冠廷',
             '范玮琪', '谭维维', '马吟吟', '张栋梁', '张智霖', '卫兰', '田震', '郭富城', '李玖哲', '丁当', '腾格尔',
             '宇多田光', '詹雯婷', '青鸟飞鱼', '黄小琥', '巫启贤', '韦礼安', '邱永传', '品冠', '阿兰', '齐一', '曹格',
             '蔡国庆', '龚琳娜', '方力申', '乌兰', '卓文萱', '曲旦卓玛', 'Lady Gaga', 'The Score',
             '许魏洲', '余佳运', 'Shawn Mendes', '阿冗', '木小雅', '比莉', '吴雨霏', '王贰浪']
    pool = Pool(processes=32)
    pool.map(test_xuqqiu, lists)
    pool.close()  # 关闭进程池，不再接受新的进程
    pool.join()
    # test_xuqqiu()
    pass


